﻿using System;
using System.Collections.Generic;
using System.Text;
using FieldDevice.ModBus;
using FieldLib;
using FieldLib.Comm;

namespace FieldDevice.Devices
{
    public class DeviceYaaoA1DC : DeviceDC
    {
        public DeviceYaaoA1DC(byte address)
            :base(address)
        {
            base.ButteryNumber = 9;
            base.Intialize();
            this.Intializes();

        }


        public const ushort Register_UAB = 0x1400;                //AB线电压  
        public const ushort Register_UBC = 0x1401;                 //BC线电压
        public const ushort Register_UCA = 0x1402;                  //CA线电压
        public const ushort Register_UO = 0x1403;                   //合母电压                  
        public const ushort Register_Ubat = 0x1404;                 //电池电压
        public const ushort Register_UKM = 0x1405;                  //控母电压
        public const ushort Register_UCD = 0x1406;                  //充电电流
        public const ushort Register_IKM = 0x1407;                  //控母电流
        public const ushort Register_IFD= 0x1408;                   //放电电流

        public const ushort Register_UDC_1 = 0x1440;                //1#电压
        public const ushort Register_UDC_2 = 0x1441;                //2#电压
        public const ushort Register_UDC_3 = 0x1442;                //3#电压
        public const ushort Register_UDC_4 = 0x1443;                //4#电压
        public const ushort Register_UDC_5 = 0x1444;                //5#电压
        public const ushort Register_UDC_6 = 0x1445;                //6#电压
        public const ushort Register_UDC_7 = 0x1446;                //7#电压
        public const ushort Register_UDC_8 = 0x1447;                //8#电压
        public const ushort Register_UDC_9 = 0x1448;                //9#电压

        public const ushort Register1440_SingleButterVoltageBegin = 0x1440;     //电池电压的起始寄存器
        public const ushort Register1442 = 0x1442;                              //
        public const ushort Register1443 = 0x1444;                              //
                     
        public const ushort Register_AlarmBegin = 0x1480;                       //遥信量的起始地址
        public const ushort Register_AlarmSec = 0x1481;                          //第二遥信量
        public const ushort Register_AlarmThird = 0x1482;                       //第三遥信量
        public const ushort Register_Value = 0x1400;                            //控母、合母电压

        public void Intializes()
        {
            base.RequestRegister(Register_UDC_1, 9);
            base.RequestRegister(Register_UAB, 9);    
            base.RequestRegister(Register_AlarmBegin, 3);
        }

        public override void DebugReads()
        {

            if (DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
                this.UpdateReads();
            else
                base.DebugReads();
            
                //UpdateReads();  
        }

        public void UpdateReads()               //更新直流屏中所有参数的读数
        {
            ReadTelemeteringValues(Register_UAB, 9);
            ReadAlaram(Register_AlarmBegin, 3);
            ReadBatteryValue();
        }


        public void ReadBatteryValue()
        {
            byte[] bytes = ReadRegister(Register_UDC_1, 9);
            FieldLib.ModBusPacket packet = new FieldLib.ModBusPacket(this.ModbusAddress, this.ModBusReadCommand,
               Register_UDC_1, 9);
            TranslateRegister(packet, bytes);
        }

        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {
            ushort tmpValue = value[begin];
            tmpValue <<= 8;
            tmpValue += value[begin + 1];
            //if ((registeraddress >= Register1440_SingleButterVoltageBegin) && 
            //    registeraddress < (Register1440_SingleButterVoltageBegin + base.ButteryNumber))
            //{
            //    int batNo = registeraddress - Register1440_SingleButterVoltageBegin;
            //    float volt = tmpValue / 100.0f;
            //    base.ButteryVoltages[batNo] = volt;
                switch (registeraddress)
                {
                    case Register_UDC_1:
                        RecordRead("1#", tmpValue/100.0f);
                        break;
                    case Register_UDC_2:
                        RecordRead("2#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_3:
                        RecordRead("3#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_4:
                        RecordRead("4#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_5:
                        RecordRead("5#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_6:
                        RecordRead("6#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_7:
                        RecordRead("7#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_8:
                        RecordRead("8#", tmpValue / 100.0f);
                        break;
                    case Register_UDC_9:
                        RecordRead("9#", tmpValue / 100.0f);
                        break;
                    case Register_UAB:
                        RecordRead("ZLP_UAB", tmpValue/10.0f);
                        break;
                    case Register_UBC:
                        RecordRead("ZLP_UBC", tmpValue/10.0f);
                        break;
                    case Register_UCA:
                        RecordRead("ZLP_UCA", tmpValue/10.0f);
                        break;
                    case Register_UO:
                        RecordRead("ZLP_U0", tmpValue/10.0f);
                        break;
                    case Register_Ubat:
                        RecordRead("ZLP_Ubat", tmpValue/10.0f);
                        break;
                    case Register_UKM:
                        RecordRead("ZLP_UKM", tmpValue/10.0f);
                        break;
                    case Register_UCD:
                        RecordRead("ZLP_UCD", tmpValue);
                        break;
                    case Register_IKM:
                        RecordRead("ZLP_IKM", tmpValue);
                        break;
                    case Register_IFD:
                        RecordRead("ZLP_IFD", tmpValue);
                        break;
                    case Register_AlarmBegin:
                        if ((tmpValue & 0x0001) != 0)
                            RecordRead("ZLP_Alaram_1", 1);
                        else
                            RecordRead("ZLP_Alaram_1", 0);
                        if ((tmpValue & 0x0002) != 0)
                            RecordRead("ZLP_Alaram_2", 1);
                        else
                            RecordRead("ZLP_Alaram_2", 0);
                        if ((tmpValue & 0x0004) != 0)
                            RecordRead("ZLP_Alaram_3", 1);
                        else
                            RecordRead("ZLP_Alaram_3", 0);
                        if ((tmpValue & 0x0008) != 0)
                            RecordRead("ZLP_Alaram_4", 1);
                        else
                            RecordRead("ZLP_Alaram_4", 0);
                        if ((tmpValue & 0x0010) != 0)
                            RecordRead("ZLP_Alaram_5", 1);
                        else
                            RecordRead("ZLP_Alaram_5", 0);
                        if ((tmpValue & 0x0020) != 0)
                            RecordRead("ZLP_Alaram_6", 1);
                        else
                            RecordRead("ZLP_Alaram_6", 0);
                        if ((tmpValue & 0x0040) != 0)
                            RecordRead("ZLP_Alaram_7", 1);
                        else
                            RecordRead("ZLP_Alaram_7", 0);
                        if ((tmpValue & 0x0080) != 0)
                            RecordRead("ZLP_Alaram_8", 1);
                        else
                            RecordRead("ZLP_Alaram_8", 0);
                        if ((tmpValue & 0x0100) != 0)
                            RecordRead("ZLP_Alaram_9", 1);
                        else
                            RecordRead("ZLP_Alaram_9", 0);
                        if ((tmpValue & 0x0200) != 0)
                            RecordRead("ZLP_Alaram_10", 1);
                        else
                            RecordRead("ZLP_Alaram_10", 0);
                        if ((tmpValue & 0x0400) != 0)
                            RecordRead("ZLP_Alaram_11", 1);
                        else
                            RecordRead("ZLP_Alaram_11", 0);
                        if ((tmpValue & 0x0800) != 0)
                            RecordRead("ZLP_Alaram_12", 1);
                        else
                            RecordRead("ZLP_Alaram_12", 0);
                        if ((tmpValue & 0x1000) != 0)
                            RecordRead("ZLP_Alaram_13", 1);
                        else
                            RecordRead("ZLP_Alaram_13", 0);
                        if ((tmpValue & 0x2000) != 0)
                            RecordRead("ZLP_Alaram_14", 1);
                        else
                            RecordRead("ZLP_Alaram_14", 0);
                        if ((tmpValue & 0x4000) != 0)
                            RecordRead("ZLP_Alaram_15", 1);
                        else
                            RecordRead("ZLP_Alaram_15", 0);
                        if ((tmpValue & 0x8000) != 0)
                            RecordRead("ZLP_Alaram_16", 1);
                        else
                            RecordRead("ZLP_Alaram_16", 0);
                        break;
                    case Register_AlarmSec:
                        if ((tmpValue & 0x0001) != 0)
                            RecordRead("ZLP_Alaram_17", 1);
                        else
                            RecordRead("ZLP_Alaram_17", 0);
                        if ((tmpValue & 0x0002) != 0)
                            RecordRead("ZLP_Alaram_18", 1);
                        else
                            RecordRead("ZLP_Alaram_18", 0);
                        if ((tmpValue & 0x0004) != 0)
                            RecordRead("ZLP_Alaram_19", 1);
                        else
                            RecordRead("ZLP_Alaram_19", 0);
                        if ((tmpValue & 0x0008) != 0)
                            RecordRead("ZLP_Alaram_20", 1);
                        else
                            RecordRead("ZLP_Alaram_20", 0);
                        if ((tmpValue & 0x0010) != 0)
                            RecordRead("ZLP_Alaram_21", 1);
                        else
                            RecordRead("ZLP_Alaram_21", 0);
                        if ((tmpValue & 0x0020) != 0)
                            RecordRead("ZLP_Alaram_22", 1);
                        else
                            RecordRead("ZLP_Alaram_22", 0);
                        if ((tmpValue & 0x0040) != 0)
                            RecordRead("ZLP_Alaram_23", 1);
                        else
                            RecordRead("ZLP_Alaram_23", 0);
                        if ((tmpValue & 0x0080) != 0)
                            RecordRead("ZLP_Alaram_24", 1);
                        else
                            RecordRead("ZLP_Alaram_24", 0);
                        break;
                    case Register_AlarmThird:
                        if ((tmpValue & 0x0002) != 0)
                            RecordRead("ZLP_Alaram_25", 1);
                        else
                            RecordRead("ZLP_Alaram_25", 0);
                        break;
                    
                    default:
                        break;            
                //}
                //return volt;
            }

            //switch (registeraddress)
            //{
            //    default:
            //        throw new Exception("Unhandled register " + registeraddress + "!");
            //}
            return 0.0f;
        }

        public void ReadTelemeteringValues(ushort registeraddress, ushort datanumber)
        {

            byte[] results = base.ReadRegister(registeraddress, datanumber);
            CheckOut(results);
            ushort tmpValue = 0;
            for (int i = 0; i < datanumber; i++)
            {
                tmpValue = results[3 + 2 * i];
                tmpValue <<= 8;
                tmpValue += results[4 + 2 * i];
                switch (i)
                { 
                    case 0:
                        RecordRead("ZLP_UAB", tmpValue);
                        break;
                    case 1:
                        RecordRead("ZLP_UBC", tmpValue);
                        break;
                    case 2:
                        RecordRead("ZLP_UCA", tmpValue);
                        break;
                    case 3:
                        RecordRead("ZLP_U0", tmpValue);
                        break;
                    case 4:
                        RecordRead("ZLP_Ubat", tmpValue);
                        break;
                    case 5:
                        RecordRead("ZLP_UKM", tmpValue);
                        break;
                    case 6:
                        RecordRead("ZLP_UCD", tmpValue);
                        break;
                    case 7:
                        RecordRead("ZLP_IKM", tmpValue);
                        break;
                    case 8:
                        RecordRead("ZLP_IFD", tmpValue);
                        break;

                    default:
                        break;
                }

            }
            
        }

        public void ReadAlaram(ushort registeraddress, ushort datanumber)
        {
            byte[] results = base.ReadRegister(registeraddress, datanumber);
            CheckOut(results);
             ushort tmpValue = 0;
             for (int i = 0; i < datanumber; i++)
             {
                 tmpValue = results[3 + 2 * i];
                 tmpValue <<= 8;
                 tmpValue += results[4 + 2 * i];
                 switch (registeraddress)
                 { 
                     case Register_AlarmBegin:
                         if ((tmpValue & 0x0001) != 0)
                             RecordRead("ZLP_Alaram_1", 1);
                         else
                             RecordRead("ZLP_Alaram_1", 0);
                         if ((tmpValue & 0x0002) != 0)
                             RecordRead("ZLP_Alaram_2", 1);
                         else
                             RecordRead("ZLP_Alaram_2", 0);
                         if ((tmpValue & 0x0004) != 0)
                             RecordRead("ZLP_Alaram_3", 1);
                         else
                             RecordRead("ZLP_Alaram_3", 0);
                         if ((tmpValue & 0x0008) != 0)
                             RecordRead("ZLP_Alaram_4", 1);
                         else
                             RecordRead("ZLP_Alaram_4", 0);
                         if ((tmpValue & 0x0010) != 0)
                             RecordRead("ZLP_Alaram_5", 1);
                         else
                             RecordRead("ZLP_Alaram_5", 0);
                         if ((tmpValue & 0x0020) != 0)
                             RecordRead("ZLP_Alaram_6", 1);
                         else
                             RecordRead("ZLP_Alaram_6", 0);
                         if ((tmpValue & 0x0040) != 0)
                             RecordRead("ZLP_Alaram_7", 1);
                         else
                             RecordRead("ZLP_Alaram_7", 0);
                         if ((tmpValue & 0x0080) != 0)
                             RecordRead("ZLP_Alaram_8", 1);
                         else
                             RecordRead("ZLP_Alaram_8", 0);
                         if ((tmpValue & 0x0100) != 0)
                             RecordRead("ZLP_Alaram_9", 1);
                         else
                             RecordRead("ZLP_Alaram_9", 0);
                         if ((tmpValue & 0x0200) != 0)
                             RecordRead("ZLP_Alaram_10", 1);
                         else
                             RecordRead("ZLP_Alaram_10", 0);
                         if ((tmpValue & 0x0400) != 0)
                             RecordRead("ZLP_Alaram_11", 1);
                         else
                             RecordRead("ZLP_Alaram_11", 0);
                         if ((tmpValue & 0x0800) != 0)
                             RecordRead("ZLP_Alaram_12", 1);
                         else
                             RecordRead("ZLP_Alaram_12", 0);
                         if ((tmpValue & 0x1000) != 0)
                             RecordRead("ZLP_Alaram_13", 1);
                         else
                             RecordRead("ZLP_Alaram_13", 0);
                         if ((tmpValue & 0x2000) != 0)
                             RecordRead("ZLP_Alaram_14", 1);
                         else
                             RecordRead("ZLP_Alaram_14", 0);
                         if ((tmpValue & 0x4000) != 0)
                             RecordRead("ZLP_Alaram_15", 1);
                         else
                             RecordRead("ZLP_Alaram_15", 0);
                         if ((tmpValue & 0x8000) != 0)
                             RecordRead("ZLP_Alaram_16", 1);
                         else
                             RecordRead("ZLP_Alaram_16", 0);

                         break;
                     case Register_AlarmBegin+1:
                         if ((tmpValue & 0x0001) != 0)
                             RecordRead("ZLP_Alaram_17", 1);
                         else
                             RecordRead("ZLP_Alaram_17", 0);
                         if ((tmpValue & 0x0002) != 0)
                             RecordRead("ZLP_Alaram_18", 1);
                         else
                             RecordRead("ZLP_Alaram_18", 0);
                         if ((tmpValue & 0x0004) != 0)
                             RecordRead("ZLP_Alaram_19", 1);
                         else
                             RecordRead("ZLP_Alaram_19", 0);
                         if ((tmpValue & 0x0008) != 0)
                             RecordRead("ZLP_Alaram_20", 1);
                         else
                             RecordRead("ZLP_Alaram_20", 0);
                         if ((tmpValue & 0x0010) != 0)
                             RecordRead("ZLP_Alaram_21", 1);
                         else
                             RecordRead("ZLP_Alaram_21", 0);
                         if ((tmpValue & 0x0020) != 0)
                             RecordRead("ZLP_Alaram_22", 1);
                         else
                             RecordRead("ZLP_Alaram_22", 0);
                         if ((tmpValue & 0x0040) != 0)
                             RecordRead("ZLP_Alaram_23", 1);
                         else
                             RecordRead("ZLP_Alaram_23", 0);
                         if ((tmpValue & 0x0080) != 0)
                             RecordRead("ZLP_Alaram_24", 1);
                         else
                             RecordRead("ZLP_Alaram_24", 0);
                         break;
                     case Register_AlarmBegin+2:
                         if ((tmpValue & 0x0002) != 0)
                             RecordRead("ZLP_Alaram_25", 1);
                         else
                             RecordRead("ZLP_Alaram_25", 0);
                         break;
                     default:
                         break;
                 }
             }
        
        }

    }
}
